package com.hydl.power.utils;


import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;

public class MainDemo {

  private final static Logger logger = LoggerFactory.getLogger(MainDemo.class);
  public static void main(String[] args) {
    // lat 31.2990170   纬度
    //log 121.3466440    经度
    String add = MainDemo.getAdd("34.761615", "113.724696");
    logger.info(add);

  }
  /**
   *根据经纬度获取省市区
   * @param log
   * @param lat
   * @return
   */
  public static String getAdd(String log, String lat ){
    //lat 小  log  大
    //参数解释: 纬度,经度 采用高德API可参考高德文档https://lbs.amap.com/
    //注意key是在高德开放平台申请的key,具体获得key的步骤请查看网址:https://developer.amap.com/api/webservice/guide/create-project/get-key
    String key = "32ffb2127e00b63df88db413bf8b8cf2";
    String urlString = "https://restapi.amap.com/v3/geocode/regeo?location="+lat+","+log+"&extensions=base&batch=false&roadlevel=0&key="+key;
    String res = "";
    try {
      URL url = new URL(urlString);
      java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");
      java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
      String line;
      while ((line = in.readLine()) != null) {
        res += line+"\n";
      }
      in.close();
      //解析结果
      JSONObject jsonObject = JSONObject.parseObject(res);
      logger.info(jsonObject.toJSONString());
      JSONObject jsonObject1 = jsonObject.getJSONObject("regeocode");
      res =jsonObject1.getString("formatted_address");
    } catch (Exception e) {
      logger.error("获取地址信息异常{}",e.getMessage());
      return null;
    }
    System.out.println("通过API获取到具体位置:"+res);
    return res;
  }

}
